<?php

namespace Admin\Controller;
use Think\Controller;
class SiteController extends BaseController {

	public function pk10(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}

		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}

	public function agent(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}

		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}

	public function xiazhu_bj28_1(){

		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}




	public function siteeditpost(){
	    $db=M('admin_config');
	    $data=$db->where('id=1')->find();
        $zj=$db->where('id=2')->find();
        if(IS_POST){
            //修改的时候重置时间0点然后下面被动执行一次
            $today = strtotime(date("Y-m-d"),time());
            $stat=array(
                'peopleNumber'=>$_POST['peopleNumber'],
                'moneyNumber'=>$_POST['moneyNumber'],
                'startTime'=>$today,
                'endTime'=>time(),
            );
            $res=$db->where('id=1')->setField($stat);
            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            //当天0点时间戳
            $today = strtotime(date("Y-m-d"),time());
            //24点
            $peopleNumber =  $zj['peoplenumber']+ $data['peoplenumber'];
            $moneyNumber =  $zj['moneynumber']+$data['moneynumber'];
                $dataconfig=array(
                    'startTime' =>time(),
                    'endTime'=>$today+60*60*24
                );
                $res=$db->where('id=1')->setField($dataconfig);
                if($res){
                    $sitedata=array(
                        'peopleNumber'=>  $peopleNumber,
                        'moneyNumber'=>   $moneyNumber
                    );
                 $db->where('id=2')->setField($sitedata);
                }


        }
        $this->assign('data',$data);
        $this->display();
    }
	public function xiazhu_bj28_2(){

		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}

	public function xiazhu_bj28_3(){

		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}

	public function xiazhu_jnd28_1(){
		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}

	public function xiazhu_jnd28_2(){
		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}

	public function xiazhu_jnd28_3(){
		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}

    public function xiazhu_jnd28_4(){
        if(IS_POST){
            $this -> sitesave('site.php');
        }else{
            $this->display();
        }
    }

	public function rule_bj28_1(){
		if(IS_POST){
			$content =  htmlspecialchars_decode(I('content',false));

			$res = M('config')->where("id=4")->save(array('kefu'=>$content));
			if ($res) {
				$this->success('保存成功');
			}else{
				$this->error("保存失败");
			}
		}else{
			$config = M('config')->where("id=4")->find();
			$this->assign("config",$config);
			$this->display();
		}
	}

	public function rule_bj28_2(){
		if(IS_POST){
			$content =  htmlspecialchars_decode(I('content',false));

			$res = M('config')->where("id=5")->save(array('kefu'=>$content));
			if ($res) {
				$this->success('保存成功');
			}else{
				$this->error("保存失败");
			}
		}else{
			$config = M('config')->where("id=5")->find();
			$this->assign("config",$config);
			$this->display();
		}
	}

	public function rule_bj28_3(){
		if(IS_POST){
			$content =  htmlspecialchars_decode(I('content',false));

			$res = M('config')->where("id=6")->save(array('kefu'=>$content));
			if ($res) {
				$this->success('保存成功');
			}else{
				$this->error("保存失败");
			}
		}else{
			$config = M('config')->where("id=6")->find();
			$this->assign("config",$config);
			$this->display();
		}
	}

    public function rule_bj28_4(){
        if(IS_POST){
            $content =  htmlspecialchars_decode(I('content',false));

            $res = M('config')->where("id=10")->save(array('kefu'=>$content));
            if ($res) {
                $this->success('保存成功');
            }else{
                $this->error("保存失败");
            }
        }else{
            $config = M('config')->where("id=6")->find();
            $this->assign("config",$config);
            $this->display();
        }
    }

	public function rule_jnd28_1(){
		if(IS_POST){
			$content =  htmlspecialchars_decode(I('content',false));

			$res = M('config')->where("id=7")->save(array('kefu'=>$content));
			if ($res) {
				$this->success('保存成功');
			}else{
				$this->error("保存失败");
			}
		}else{
			$config = M('config')->where("id=7")->find();
			$this->assign("config",$config);
			$this->display();
		}
	}

	public function rule_jnd28_2(){
		if(IS_POST){
			$content =  htmlspecialchars_decode(I('content',false));

			$res = M('config')->where("id=8")->save(array('kefu'=>$content));
			if ($res) {
				$this->success('保存成功');
			}else{
				$this->error("保存失败");
			}
		}else{
			$config = M('config')->where("id=8")->find();
			$this->assign("config",$config);
			$this->display();
		}
	}

	public function rule_jnd28_3(){
		if(IS_POST){
			$content =  htmlspecialchars_decode(I('content',false));

			$res = M('config')->where("id=9")->save(array('kefu'=>$content));
			if ($res) {
				$this->success('保存成功');
			}else{
				$this->error("保存失败");
			}
		}else{
			$config = M('config')->where("id=9")->find();
			$this->assign("config",$config);
			$this->display();
		}
	}

    public function rule_jnd28_4(){
        if(IS_POST){
            $content =  htmlspecialchars_decode(I('content',false));

            $res = M('config')->where("id=11")->save(array('kefu'=>$content));
            if ($res) {
                $this->success('保存成功');
            }else{
                $this->error("保存失败");
            }
        }else{
            $config = M('config')->where("id=9")->find();
            $this->assign("config",$config);
            $this->display();
        }
    }

	public function message(){
		$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
		if (!$auth) {
			echo "未授权或授权已过期";exit;
		}

		if(IS_POST){
			$this -> sitesave('site.php');
		}else{
			$this->display();
		}
	}

	public function index() {
		if (IS_POST) {
			$this -> sitesave('site.php');
		} else {
			// $auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
			$auth = auth_check(C('auth_code'),$_SERVER['HTTP_HOST']);
			$this->assign('auth',$auth);
			// var_dump($auth);exit;
			$this -> display();
		}
	}

	public function setting() {
		if (IS_POST) {
			$this -> sitesave('route.php');
		} else {
			$this -> display();
		}
	}

	private function sitesave($filename) {

		if ($this -> update_config($_POST, $filename)) {

			$this -> success('修改成功，跳转中~', U('site/' . ACTION_NAME));

		} else {

			$this -> error('操作失败', U('site/' . ACTION_NAME));

		}
	}

	private function update_config($new_config, $filename) {
		if (isset($new_config['min_point'])) {
			$log = array(
				'username' => session('admin')['username'],
				'type' => 4,
				'addtime' => time(),
				'content' => "修改下注配置倍率"
			);
			M('admin_log')->add($log);
		}


		$config_file = CONF_PATH . $filename;
		if (is_file($config_file) && is_writable($config_file)) {

			$config =
			require $config_file;

			$config = array_merge($config, $new_config);

			file_put_contents($config_file, "<?php \nreturn " . stripslashes(var_export($config, true)) . ";", LOCK_EX);

			@unlink(RUNTIME_FILE);

			return true;

		} else {
			return false;
		}

	}

	public function gxcode(){
		$auth_url = "http://auth.httx06.cn/Home/Index/authcheck?auth_code=9STWmr4ENbk0dQalLpRIN12N93tf5KMQ";
		$auth = $_SERVER['SERVER_NAME'];
//		$auth = trim($auth,chr(239).chr(187).chr(191));
//		$auth = json_decode($auth,true);
		if (!$auth) {
			$this->error();
		}
		  // $url = "http://".$auth['site_url']."/Home/get/go?auth_code=".C('auth_code');
         $url="http://".$auth;

		$img = qrcode($url,$level=3,$size=4,true);
		$code_arr = array('forever_qrcode'=>"/".$img);
		if ($this -> update_config($code_arr, 'site.php')) {

			$this -> success('更新成功，跳转中~');

		} else {

			$this -> error('更新失败');

		}
	}

	public function agent_url(){
		$list = M('user')->where("is_agent = 1")->select();
		$auth_url = "http://auth.httx06.cn/Home/Index/authcheck?auth_code=9STWmr4ENbk0dQalLpRIN12N93tf5KMQ";
		$auth = curlGet($auth_url);
		$auth = trim($auth,chr(239).chr(187).chr(191));
		$auth = json_decode($auth,true);
		if (!$auth) {
			$this->error();
		}

		foreach ($list as $key => $value) {
			$url = "http://".$auth['site_url']."/Home/get/go?auth_code=".C('auth_code')."&t_id=".$value['id'];
			$img = qrcode($url);
			$res = M('user')->where("id={$value['id']}")->save(array("qrcode"=>"/".$img));
		}
		$this -> success('更新成功，跳转中~');
	}

	public function agent_url_id(){
		$auth_url = "http://auth.httx06.cn/Home/Index/authcheck?auth_code=9STWmr4ENbk0dQalLpRIN12N93tf5KMQ";
		$auth = curlGet($auth_url);
		$auth = trim($auth,chr(239).chr(187).chr(191));
		$auth = json_decode($auth,true);
		if (!$auth) {
			$this->error();
		}

		$id = I('id');
		$url = "http://".$auth['site_url']."/Home/get/go?auth_code=".C('auth_code')."&t_id=".$id;
		$img = qrcode($url);
		$res = M('user')->where("id={$id}")->save(array("qrcode"=>"/".$img));

		$this -> success('更新成功，跳转中~');
	}

}